using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using SPCoder.Scripting;

namespace SPCoderWP.UI.WebControls.WebParts
{
    [Guid("caf5f52d-0547-49a0-91d9-d115242aff12")]
    public class SPCoderWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        
        Control _spCoderControl;
        IronPythonEngine ironPythonEngine = new IronPythonEngine();
        

        public SPCoderWebPart()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {

                    base.CreateChildControls();

                    string controlUrl = "~/_controltemplates/SPCoder/SPCoder.ascx";

                    _spCoderControl = this.LoadControl(controlUrl);
                    ((Button)_spCoderControl.FindControl("btnExecute")).Click += new EventHandler(Execute_Click);
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        void Execute_Click(object sender, EventArgs e)
        {
            try
            {
                string rez = "";
                IDictionary<string, object> variables = new Dictionary<string, object>();
                variables.Add("rez", rez);
                variables.Add("txtResult", ((TextBox)_spCoderControl.FindControl("txtResult")));

                //variables.Add("main", this);
                ironPythonEngine.SetVariables(variables);
                string code = ((TextBox)_spCoderControl.FindControl("txtCode")).Text;
                object result = ironPythonEngine.Execute(code);

                //((TextBox)_spCoderControl.FindControl("txtResult")).Text = result as string;
            }
            catch (Exception exc)
            {
                ((TextBox)_spCoderControl.FindControl("txtResult")).Text = exc.ToString();
            }
            finally
            { }
        }
        private Control LoadControl(string controlUrl)
        {
            Control control = Page.LoadControl(controlUrl);
            this.Controls.Add(control);
            return control;
        }
        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    }
}
